package net.htjs.mobile.gzwoa;

import net.htjs.mobile.gzwoa.util.L;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {


    protected WebView webView;
    static String LX="";
    ProgressDialog progressDialog, xh_pDialog;
    static int xh_count;
    // 获取SD卡目录
   
    // 提示信息对话框控制对象
    final GetListHandler handler = new GetListHandler();
    
    protected static String dowloadDir = Environment.getExternalStorageDirectory() + "/ideasdownload/";
    
    protected String filePath = "";
    
    
    
    class GetListHandler extends Handler {
            @Override
            public void handleMessage(Message msg) {
                    progressDialog.dismiss();// 关闭进度条
            }
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (event.getAction() == KeyEvent.ACTION_DOWN) {
			switch (keyCode) {
			case KeyEvent.KEYCODE_BACK:
				if(webView.canGoBack()){
					webView.goBack();
					return true;
				}
             
            default:
            	break;
			}
		}
		return super.onKeyDown(keyCode, event);
	}
    public void callPhone(String url){
    	Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse(url));
	    
    	MainActivity.this.startActivity(intent);
    }
    @SuppressLint("NewApi")
	public void onCreate(Bundle b) {
            super.onCreate(b);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            this.setContentView(R.layout.activity_main);

            webView = (WebView) this.findViewById(R.id.webviewFirst);
            webView.getSettings().setJavaScriptEnabled(true);

            
            // 提示信息对话框初始化
            progressDialog = new ProgressDialog(MainActivity.this);
            progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
           // progressDialog.setTitle("请稍等");
            progressDialog.setMessage("正在读取数据!");
            //progressDialog.set

           

            // 显示提示信息对话框
            progressDialog.show();

            // 设置webView客户端加载对象（为默认浏览器组件）
            webView.setWebViewClient(new WebViewClient() {
            		
            		
                    @Override
                    public boolean shouldOverrideUrlLoading(WebView view, String url) {
                            
                    	if(url.toLowerCase().startsWith("tel:")){
                        	callPhone(url);
                        	//Util.dialogProcess(getApplicationContext(), "拨打电话 "+url.replace("tel:", ""), "拨打", "取消", ListGwWebView.this, "callPhone", new Object[]{url}, null, null, null);
                        	return true;
                        }
                            view.loadUrl(url);
                            return true;
                    }

                    // 页面加载前调方法定义
                    @Override
                    public void onPageStarted(WebView view, String url, Bitmap favicon) {

                            if (!progressDialog.isShowing())
                                    progressDialog.show();

                            L.l(url + " ------------------------------------");
                            
                            if(url.toLowerCase().startsWith("tel:")){
                            	callPhone(url);
                            	//Util.dialogProcess(getApplicationContext(), "拨打电话 "+url.replace("tel:", ""), "拨打", "取消", ListGwWebView.this, "callPhone", new Object[]{url}, null, null, null);
                            	return ;
                            }
                           
                            super.onPageStarted(view, url, favicon);
                    }

                    // 页面加载回调方法定义
                    public void onPageFinished(WebView view, String url) {
                            // TODO Auto-generated method stub
                            new Thread(new Runnable() {

                                    public void run() {
                                            // TODO Auto-generated method stub
                                            Message msg = handler.obtainMessage();
                                            handler.sendMessage(msg);
                                    }

                            }).start();
                            super.onPageFinished(view, url);
                            //webView.loadUrl("javascript:" + "alert(123);");
                    }

            });
           
            // 得到上个activity传入的参数信息
           // Bundle bundle = this.getIntent().getExtras();
           
            //String lx = bundle.getString(L.LX);
            //LX=lx;
            /*Cookie sessionCookie=null;
            if(LX.equals(L.Nine.DZYX)){
            	sessionCookie = L.mailcookie;
            }else{
            	sessionCookie = L.cookie;
            }
            // 这里的cookie就是上面保存的cookie，
            // 设置模拟cookie，提供客户端服务器认证凭证
            CookieSyncManager.createInstance(this);
            CookieManager cookieManager = CookieManager.getInstance();
            if (sessionCookie != null) {
                    // cookieManager.removeSessionCookie();
                    String cookieString = sessionCookie.getName() + "="
                                    + sessionCookie.getValue() + "; domain="
                                    + sessionCookie.getDomain();
                    cookieManager.setCookie(L.baseDomain, cookieString);
                    CookieSyncManager.getInstance().sync();
            }*/

           /*
            if (L.Nine.GW.endsWith(lx) || L.Nine.XW.equals(lx)) {
                    String zt = bundle.getString(L.ZT);
                    URL = L.gwListWapUrl + "&ZT=" + zt + "&CZRY_DM="
                                    + L.status.get("CZRY_DM") + "&JSESSIONID="
                                    + L.status.get("JSESSIONID");
            } else if (L.Nine.DZGG.endsWith(lx)) {
                    URL = L.dzggUrl + "&CZRY_DM=" + L.status.get("CZRY_DM")
                                    + "&JSESSIONID=" + L.status.get("JSESSIONID");
            } else if (L.Nine.DZYX.endsWith(lx)) {
                    URL = L.dzyxUrl+L.Nine.MAILSESSIONID;

            } else if (L.Nine.GZZD.endsWith(lx)) {
                    URL = L.gzzdUrl + "&CZRY_DM=" + L.status.get("CZRY_DM")
                                    + "&JSESSIONID=" + L.status.get("JSESSIONID");
            } else if (L.Nine.TXL.endsWith(lx)) {
                    URL = L.txlUrl + "&CZRY_DM=" + L.status.get("CZRY_DM")
                                    + "&JSESSIONID=" + L.status.get("JSESSIONID");
            }*/
            String URL =L.loginUrl;
            webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
            webView.loadUrl(URL, L.status);
            System.out.println(L.cookie);

    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
